#include <iostream>
#include <stdlib.h>
#include <algorithm>

#include "libmri.h"

using namespace std;

int main (int argc, char **argv)
{
	shm_array_t *shm_K = 0, size_KKt, *shm_KKt = 0;

	if (0 == shm_K){
		if (get_shm_array("K", &shm_K)){
			perror("K");
			return -1;
		}
	}

	int ks1 = shm_K->dims[0],
	    ks2 = shm_K->dims[1],
	    ks3 = shm_K->dims[2],
	      n = shm_K->dims[3],
	      m = shm_K->dims[4];

	size_KKt.type = shm_K->type;
	size_KKt.ndims = 5;
	size_KKt.dims[0] = 2*ks1-1;
	size_KKt.dims[1] = 2*ks2-1;
	size_KKt.dims[2] = 2*ks3-1;
	size_KKt.dims[3] = n;
	size_KKt.dims[4] = n;

	if (make_shm_array ("KKt", &size_KKt, &shm_KKt)){
		perror("KKt");
		return -1;
	}

	cplxf *K = (cplxf*) shm_K->data.c;
	cplxf *KKt = (cplxf*) shm_KKt->data.c;

	int ksize[3] = {ks1, ks2, ks3};

	double ts = timestamp();
	kreduce3d (KKt, K, ksize, n, m);
	cout << (timestamp() - ts) << " s" << endl;

	return 0;
}
